#include <exception>
#include "Logger.h"
class SystemException : public std::exception {
public:
    SystemException(const std::string& context, const std::string& details)
        : msg("[" + context + "] " + details) {}
    const char* what() const noexcept override {
        return msg.c_str();
    }
private:
    std::string msg;
};
void globalExceptionHandler() {
    std::set_terminate([](){
        try {
            auto exPtr = std::current_exception();
            if (exPtr) {
                std::rethrow_exception(exPtr);
            }
        } catch (const std::exception& e) {
            Logger::logError("Unhandled exception: " + std::string(e.what()));
            std::cerr << "Critical Error: " << e.what() << std::endl;
        }
        std::abort();
    });
}
// 使用示例：
void DeviceManager::updateFirmware(int deviceId) {
    try {
        auto device = getDevice(deviceId);
        device->performFirmwareUpdate();
    } catch (const DeviceTimeoutException& e) {
        Logger::logError("Device update timeout: " + std::to_string(deviceId));
        throw SystemException("FirmwareUpdate", e.what());
    } catch (const std::exception& e) {
        throw SystemException("DeviceOperation", e.what());
    }
}